//倒计时
var timeS = document.querySelector('.timeS');
var hour = document.querySelector('.hour');
// console.log(hour);
var minute = document.querySelector(".minute");
var second = document.querySelector(".second");
//  showHourInfo(date);
function showHourInfo(time) {
    var now = new Date();  //获取现在的时间
    var nowMS = now.getTime();//将现在的时间转化为毫秒形式
    var endMS = time;//将自己定义的时间点转换为毫秒;
    MS = endMS - nowMS;
    hour.innerHTML = (Math.floor(MS / 1000 / 60 / 60)) < 10 ? '0' + Math.floor(MS / 1000 / 60 / 60) : Math.floor(MS / 1000 / 60 / 60);
    minute.innerHTML = (Math.floor((MS / 1000 / 60) % 60)) < 10 ? '0' + Math.floor((MS / 1000 / 60) % 60) : Math.floor((MS / 1000 / 60) % 60);
    second.innerHTML = (Math.floor((MS / 1000) % 60)) < 10 ? '0' + Math.floor((MS / 1000) % 60) : Math.floor((MS / 1000) % 60);
}
var times = setInterval(function () {
    var date = new Date();
    var h = date.getHours();
    h % 2 == 0 ? h = h + 2 : h = h + 1;
    date.setHours(h)
    date.setMinutes('0');
    date.setSeconds('0');
    showHourInfo(date);
    timeS.innerHTML = parseInt(h - 2);
}, 1000);
/****** 轮播图 **** */


let _this;
class Lo {
    constructor() {
        this.oldIndex = 0;
        this.newIndex = 0;
        _this = this;
        //获取节点
        this.box = document.querySelector('.grid-col2-t')
        this.uls = document.querySelector('.t-img')
        this.lisObj = document.querySelectorAll('.t-img li')
        this.ols = document.querySelector('.circle')
        this.oliObj = document.querySelectorAll('.circle li')
        this.btnL = document.querySelector('.arrow-l')
        this.btnR = document.querySelector('.arrow-r')
        this.xun()
        this.leFn()
        this.roFn()
        this.ding()
        this.yi()
        this.chu()
        //用户信息调用
        this.getData()
    }
    //遍历循环绑定事件
    xun() {
        this.oliObj.forEach((li, k) => {
            this.oliObj[k].onclick = this.olFn.bind(li, k)
            //console.log(this, k);
        })
    }
    //ol绑定事件
    olFn(k) {
        _this.oldIndex = _this.newIndex
        _this.newIndex = k
        //console.log(k);
        document.querySelector('.t-img .ac').className = ''
        document.querySelector('.circle .ac').className = ''
        _this.lisObj[_this.newIndex].className = 'ac'
        _this.oliObj[_this.newIndex].className = 'ac'
    }
    //左按钮
    leFn() {
        this.btnL.onclick = function () {
            _this.oldIndex = _this.newIndex
            console.log(_this.newIndex);
            if (_this.newIndex == 0) {
                _this.newIndex = _this.lisObj.length - 1
            } else {
                _this.newIndex--
            }
            document.querySelector('.t-img .ac').className = ''
            document.querySelector('.circle .ac').className = ''
            _this.lisObj[_this.newIndex].className = 'ac'
            _this.oliObj[_this.newIndex].className = 'ac'
        }
    }
    //右按钮
    roFn() {
        this.btnR.onclick = function () {
            _this.oldIndex = _this.newIndex
            // console.log(_this.newIndex);
            if (_this.newIndex == _this.lisObj.length - 1) {
                _this.newIndex = 0
            } else {
                _this.newIndex++
            }
            document.querySelector('.t-img .ac').className = ''
            document.querySelector('.circle .ac').className = ''
            _this.lisObj[_this.newIndex].className = 'ac'
            _this.oliObj[_this.newIndex].className = 'ac'
        }
    }
    timer;
    //设置定时器
    ding() {

        this.timer = setInterval(() => {

            this.btnR.onclick()
        }, 2000)
    }
    //鼠标移入事件
    yi() {

        this.box.onmouseover = function () {

            clearInterval(_this.timer)
        }
    }
    //移除事件
    chu() {

        this.box.onmouseout = function () {
            clearInterval(_this.timer)
            _this.ding()
        }
    }

    //发送请求 获取登录过后用户的信息
    async getData() {
        //设置请求头
        axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded';
        //token编码设置
        let token = localStorage.getItem('token')
        axios.defaults.headers.common['Authorization'] = token;
        //获取用户ID
        let Id = localStorage.getItem('user_Id')
        //发送请求
        let res = await axios.get('http://localhost:8888/users/info?id=' + Id)
        //console.log(res);
        let { data, status } = res
        let info = data.info
        //console.log(info);
        if (data.code != 1 || status != 200) return;
        if (token) {
            document.querySelector('.login').innerHTML = `<div class="login-t">
            Hi，欢迎来到京东！<br />
            <span>用户名：${info.username}</span></br>
            <span>${info.updateTime}</span>
            <a href="#" class="user_info">
                <img src="images/daidai.png" alt="">
            </a>
        </div>
            `;
            document.getElementById("price").getElementsByTagName("li")[0].innerHTML = `<li>
             <a href="#">尊贵的${info.username}</a>
             <a href="#" class="f10">欢迎来到呆呆</a>
         </li>`

        }




    }
}
new Lo()
